﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.BarterSystem.Barterables.LeaveKingdomAsClanBarterable
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System.Collections.Generic;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.Core;
using TaleWorlds.Localization;

#nullable disable
namespace TaleWorlds.CampaignSystem.BarterSystem.Barterables
{
  public class LeaveKingdomAsClanBarterable : Barterable
  {
    public override string StringID => "leave_faction_barterable";

    public override TextObject Name
    {
      get
      {
        TextObject name = new TextObject("{=x5POJVWw}Stop serving {FACTION}");
        name.SetTextVariable("FACTION", this.OriginalOwner.MapFaction.Name);
        return name;
      }
    }

    public LeaveKingdomAsClanBarterable(Hero owner, PartyBase ownerParty)
      : base(owner, ownerParty)
    {
    }

    public override int GetUnitValueForFaction(IFaction faction)
    {
      Hero leader = this.OriginalOwner.Clan.Leader;
      IFaction mapFaction = this.OriginalOwner.MapFaction;
      if (faction == this.OriginalOwner.Clan)
        return this.OriginalOwner.Clan.IsMinorFaction ? (int) Campaign.Current.Models.DiplomacyModel.GetScoreOfMercenaryToLeaveKingdom(this.OriginalOwner.Clan, this.OriginalOwner.Clan.Kingdom) : (int) Campaign.Current.Models.DiplomacyModel.GetScoreOfClanToLeaveKingdom(this.OriginalOwner.Clan, this.OriginalOwner.Clan.Kingdom);
      float unitValueForFaction;
      if (faction == this.OriginalOwner.MapFaction)
      {
        unitValueForFaction = (float) ((!this.OriginalOwner.Clan.IsUnderMercenaryService ? (double) Campaign.Current.Models.DiplomacyModel.GetScoreOfClanToLeaveKingdom(this.OriginalOwner.Clan, this.OriginalOwner.Clan.Kingdom) : (double) Campaign.Current.Models.DiplomacyModel.GetScoreOfMercenaryToLeaveKingdom(this.OriginalOwner.Clan, this.OriginalOwner.Clan.Kingdom)) * (faction == this.OriginalOwner.Clan || faction == this.OriginalOwner.Clan.Kingdom ? -1.0 : 1.0));
      }
      else
      {
        float num1 = 0.5f;
        float num2 = 0.01f;
        float num3 = -0.5f;
        float clanStrength = Campaign.Current.Models.DiplomacyModel.GetClanStrength(this.OriginalOwner.Clan);
        unitValueForFaction = !faction.IsClan || !FactionManager.IsAtWarAgainstFaction(faction, (IFaction) this.OriginalOwner.Clan.Kingdom) ? (!FactionManager.IsAlliedWithFaction(faction, (IFaction) this.OriginalOwner.Clan.Kingdom) ? clanStrength * num2 : clanStrength * num3) : clanStrength * num1;
      }
      return (int) unitValueForFaction;
    }

    public override void CheckBarterLink(Barterable linkedBarterable)
    {
    }

    public override bool IsCompatible(Barterable barterable)
    {
      return !(barterable is JoinKingdomAsClanBarterable asClanBarterable) || asClanBarterable.OriginalOwner != this.OriginalOwner || asClanBarterable.TargetKingdom != this.OriginalOwner.MapFaction;
    }

    public override ImageIdentifier GetVisualIdentifier()
    {
      return new ImageIdentifier(BannerCode.CreateFrom(this.OriginalOwner.Clan.Banner));
    }

    public override string GetEncyclopediaLink() => this.OriginalOwner.MapFaction.EncyclopediaLink;

    public override void Apply()
    {
      if (this.OriginalOwner.Clan.IsUnderMercenaryService)
        ChangeKingdomAction.ApplyByLeaveKingdomAsMercenary(this.OriginalOwner.Clan);
      else
        ChangeKingdomAction.ApplyByLeaveKingdom(this.OriginalOwner.Clan);
    }

    internal static void AutoGeneratedStaticCollectObjectsLeaveKingdomAsClanBarterable(
      object o,
      List<object> collectedObjects)
    {
      ((Barterable) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
    }
  }
}
